package vn.edu.saigontech.ncms.web.program;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import vn.edu.saigontech.ncms.model.info.Student;
import vn.edu.saigontech.ncms.model.info.Waiting;
import vn.edu.saigontech.ncms.model.program.Level;
import vn.edu.saigontech.ncms.model.program.Program;
import vn.edu.saigontech.ncms.service.LevelService;
import vn.edu.saigontech.ncms.service.ProgramService;
import vn.edu.saigontech.ncms.service.ShiftService;
import vn.edu.saigontech.ncms.service.StudentService;
import vn.edu.saigontech.ncms.service.StudentTypeService;
import vn.edu.saigontech.ncms.service.UserService;
import vn.edu.saigontech.ncms.service.WaitingService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class WaitingAction extends BaseAction {
	
	private Long id;
	private Long levelId;
	private Long studentId;
	private String registeredDate;
	private String comment;
	
	private Long programId;
	private String fromRegisteredDate;
	private String toRegisteredDate;
	private Long typeId;
	private Long consultantId;
	
	private String fromDate;
	private String toDate;
	
	public String showAdd() {
		REQUEST().setAttribute("programs", ProgramService.INSTANCE.findAllAreActive());
		REQUEST().setAttribute("student", StudentService.INSTANCE.findById(this.studentId));
		REQUEST().setAttribute("shifts", ShiftService.INSTANCE.findAll());
		return SUCCESS;
	}
	
	public String add() {
		Waiting waiting = new Waiting();
		Level level = LevelService.INSTANCE.findById(levelId);
		Student student = StudentService.INSTANCE.findById(studentId);
		Date date = Checker.isDate(this.registeredDate, this.i18n("format.date"))? DateUtil.fromString(this.registeredDate, this.i18n("format.date")) : null;
		
		waiting.setStudent(student);
		waiting.setLevel(level);
		waiting.setRegisteredDate(date);
		waiting.setComment(comment);
		waiting = WaitingService.INSTANCE.persist(waiting);
		
		this.addLoggingDetail(BaseAction.ADD, "logging.waiting.add", student.getFullName(), level.getName());
		return SUCCESS;
	}
	
	public String info() {
		Waiting waiting = WaitingService.INSTANCE.findById(this.id);
		REQUEST().setAttribute("programs", ProgramService.INSTANCE.findAllAreActive());
		REQUEST().setAttribute("waiting", waiting);
		return SUCCESS;
	}
	
	public String edit() {
		Waiting waiting = WaitingService.INSTANCE.findById(this.id);
		
		Level level = LevelService.INSTANCE.findById(levelId);
		Student student = StudentService.INSTANCE.findById(studentId);
		Date date = Checker.isDate(this.registeredDate, this.i18n("format.date"))? DateUtil.fromString(this.registeredDate, this.i18n("format.date")) : null;
		
		waiting.setStudent(student);
		waiting.setLevel(level);
		waiting.setRegisteredDate(date);
		waiting.setComment(comment);
		waiting = WaitingService.INSTANCE.persist(waiting);
		
		this.addLoggingDetail(BaseAction.ADD, "logging.waiting.edit", student.getFullName(), level.getName());
		return SUCCESS;
	}
	
	public String list() {
		REQUEST().setAttribute("level", LevelService.INSTANCE.findById(this.levelId));
		return SUCCESS;
	}
	
	public String delete() {
		Waiting waiting = WaitingService.INSTANCE.findById(this.id);
		String student = waiting.getStudent().getFullName();
		String level = waiting.getLevel().getName();
		
		WaitingService.INSTANCE.deleteById(this.id);
		this.addLoggingDetail(BaseAction.DELETE, "logging.waiting.delete", student, level);
		return SUCCESS;
	}
	
	public String deleteAll() {
		Level level = LevelService.INSTANCE.findById(this.levelId);
		LevelService.INSTANCE.findById(this.levelId).getWaitings().clear();
		this.addLoggingDetail(BaseAction.DELETE, "logging.waiting.deleteAll", level.getName());
		return SUCCESS;
	}
	
	public String showSearch() {
		Set<Level> levels = new HashSet<Level>();
		if(Checker.isNotNull(this.programId)) {
			Program program = ProgramService.INSTANCE.findById(this.programId);
			REQUEST().setAttribute("searchProgram", program);
			levels = program.getLevels();
		}
		REQUEST().setAttribute("levels", levels);
		REQUEST().setAttribute("types", StudentTypeService.INSTANCE.findAll());
		REQUEST().setAttribute("programs", ProgramService.INSTANCE.findAllAreActive());
		REQUEST().setAttribute("consultants", UserService.INSTANCE.findAllByRole("Consulting"));
		return SUCCESS;
	}
	
	public String search() {
		Waiting waiting = WaitingService.INSTANCE.createBlankWaiting();
		waiting.getStudent().setType(StudentTypeService.INSTANCE.findById(this.typeId));
		waiting.getStudent().setConsultant(UserService.INSTANCE.findById(this.consultantId));
		Level level = LevelService.INSTANCE.findById(this.levelId);
		Program program = ProgramService.INSTANCE.findById(this.programId);
		if(Checker.isNotNull(level))
			waiting.setLevel(level);
		waiting.getLevel().setProgram(program);
		waiting.setFromRegisteredDate(DateUtil.fromString(this.fromRegisteredDate, this.i18n("format.date")));
		waiting.setToRegisteredDate(DateUtil.fromString(this.toRegisteredDate, this.i18n("format.date")));
		
		SESSION().setAttribute("searchWaiting", waiting);
		this.addLoggingDetail(BaseAction.SEARCH, "logging.waiting.search");
		return SUCCESS;
	}
	
	public String showCacheSearchResult() {
		Waiting waiting = (Waiting) SESSION().getAttribute("searchWaiting");
		if(Checker.isNull(waiting))
			waiting = WaitingService.INSTANCE.createBlankWaiting();
		List<Waiting> waitings = WaitingService.INSTANCE.find(waiting);
		REQUEST().setAttribute("waitings", waitings);
		return SUCCESS;
	}
	
	public String report() {
		Date fromDateDate = DateUtil.fromString(this.fromDate, this.i18n("format.date"));
		Date toDateDate = DateUtil.fromString(this.toDate, this.i18n("format.date"));
		List<Waiting> waitings = WaitingService.INSTANCE.findByDate(fromDateDate, toDateDate);
		REQUEST().setAttribute("waitings", waitings);
		return SUCCESS;
	}
	
	public String showReportWaiting() {
		this.fromDate = DateUtil.toString(DateUtil.startOfMonth(DateUtil.newDate()), this.i18n("format.date"));
		this.toDate = DateUtil.toString(DateUtil.endOfMonth(DateUtil.newDate()), this.i18n("format.date"));
		return SUCCESS;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Long getStudentId() {
		return studentId;
	}

	public void setStudentId(Long studentId) {
		this.studentId = studentId;
	}

	public Long getLevelId() {
		return levelId;
	}

	public void setLevelId(Long levelId) {
		this.levelId = levelId;
	}

	public String getRegisteredDate() {
		return registeredDate;
	}

	public void setRegisteredDate(String registeredDate) {
		this.registeredDate = registeredDate;
	}

	public String getFromRegisteredDate() {
		return fromRegisteredDate;
	}

	public void setFromRegisteredDate(String fromRegisteredDate) {
		this.fromRegisteredDate = fromRegisteredDate;
	}

	public String getToRegisteredDate() {
		return toRegisteredDate;
	}

	public void setToRegisteredDate(String toRegisteredDate) {
		this.toRegisteredDate = toRegisteredDate;
	}

	public Long getConsultantId() {
		return consultantId;
	}

	public void setConsultantId(Long consultantId) {
		this.consultantId = consultantId;
	}

	public Long getTypeId() {
		return typeId;
	}

	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	public Long getProgramId() {
		return programId;
	}

	public void setProgramId(Long programId) {
		this.programId = programId;
	}

	public String getFromDate() {
		return fromDate;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}
}
